<?php defined('SYSPATH') OR die('No direct access allowed.');

class Forgot_Password_Controller extends Template_Controller 
{
	public $template = 'forgot_password';
	public $content = 'forgot_password_content';

	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$this->template->title = '重置密码!';
		$this->template->content->success = false;
		$this->send_password_reset();
	}

	public function send_password_reset()
	{
		if ($post = $this->input->post())
		{
			$email = $post['email'];
			if (empty($email))
			{
				$this->template->content->error = '请输入邮箱.';
				echo '请输入邮箱';
				return;
			}
			$user = ORM::factory('user');
			$find_user = $user->where('email', $email)->find();
			if (!$find_user->loaded)
			{
				$this->template->content->error = '该邮箱不存在.';
				echo '该邮箱不存在';
				return;
			}
			$md5_email_password = md5($user->email.'+'.$user->password);
			$send_string = base64_encode($user->email." &".$md5_email_password);

			$to      = $user->email;  // Address can also be array('to@example.com', 'Name')
			$from    = array('info@comefit.com' => '来健身');
			$subject = '密码重置';
			$message = "你使用了本站提供的密码找回功能，如果你确认此密码找回功能是你启用的，请点击下面的链接，按流程进行密码重设。<br><br><br>
				<a href='".url::base()."users/reset_password?p=".$send_string."'>确认密码找回</a><br/><br/>";
			 
			email::send($to, $from, $subject, $message, TRUE);
			
		}
	}
}

